<!-- 组件结构 -->
<template>
<el-row>
  <h2>1.抖动效果</h2>
  <el-col :md="{span:8,offset:6}" :xs="{span:24,offset:0}"  > 
    <el-form :model="numberValidateForm" ref="numberValidateForm" label-width="70px" class="demo-ruleForm"> 
    <el-form-item label="年龄" prop="age"  :rules="[
        { required: true, message: '年龄不能为空' },
        { type: 'number', message: '年龄必须为数字值' },
      ]">
      <el-input v-model.number="numberValidateForm.age" autocomplete="off" :class="{'apply-shake':shakeTxtName}"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('numberValidateForm')">提交</el-button>
    </el-form-item>
  </el-form> 
</el-col>
<ShakeCode/>
</el-row> 
 
</template>

<!-- 组件交互相关 -->
<script>
import ShakeCode from './ShakeCode.vue';
export default {  
  name: "Shake",
  components: { ShakeCode },
  data() {
    return {
      shakeTxtName:false,
      numberValidateForm: {
        age: "",       
        },
    };
  },
  methods: {
    submitForm(formName) {     
      this.$refs[formName].validate((valid) => {
        if (valid) {
           this.$message({
                message: '验证成功',
                type: 'success'
          });
        } 
        else {        
          this.shakeTxtName=true;
          setTimeout(()=>{
                 this.shakeTxtName=false;
          },820)
             return false;
        }
      });
    },
   
  },
};
</script>

<!-- 组件样式 -->
<style scoped>
@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0 0);
  }

  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }

  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }

  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}

.apply-shake {
  animation: shake 0.82s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

</style>

